package com.enfi.api.modules.bz.service.impl;

import com.enfi.api.configuration.SysConfig;
import com.enfi.api.modules.bz.service.FileService;
import com.enfi.api.util.SecUser;
import com.enfi.api.util.UserUtils;
import com.google.common.collect.Lists;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author wind
 * @date 2020/4/10
 */
@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private SysConfig sysConfig;
    @Override
    public void waterShow(String fileName,HttpServletRequest request, HttpServletResponse response) {
        SecUser secUser = UserUtils.getSecUser();
        String numberNo = secUser.getData().getOrDefault("numberNo","").toString();
        String waterName = "北京恩菲环保股份公司 " + numberNo + " " + secUser.getUsername();
        String p = sysConfig.getStorage().getOrDefault("file", Lists.newArrayList("")).get(0) + "/pdf/" + numberNo + "/" + fileName;
        File file = new File(p);
        if (file.exists()) {
            try {
                FileInputStream input = new FileInputStream(file);
                byte[] data = new byte[input.available()];
                input.read(data);
                response.getOutputStream().write(data);
                input.close();
                // PdfReader reader = new PdfReader(path + fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                PdfReader reader = new PdfReader(sysConfig.getStorage().getOrDefault("file", Lists.newArrayList("")).get(0) + "/pdf/base/" + fileName);
                File file1 = new File(p);
                if (!file1.exists()) {
                    FileUtils.forceMkdirParent(file1);
                }
                PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(file1));
                BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                        BaseFont.EMBEDDED);
                PdfContentByte content;
                int total = reader.getNumberOfPages() + 1;
                for (int i = 1; i < total; i++) {
                    content = stamp.getOverContent(i);
                    // 开始
                    content.beginText();
                    // 设置颜色
                    content.setColorFill(BaseColor.LIGHT_GRAY);
                    // 设置字体及字号
                    content.setFontAndSize(base, 40);
                    content.showTextAligned(Element.ALIGN_LEFT, waterName, 50, 100, 45);
                    content.endText();

                }
                stamp.close();
                reader.close();
                FileInputStream input = new FileInputStream(new File(p));
                byte[] data = new byte[input.available()];
                input.read(data);
                response.getOutputStream().write(data);
                input.close();
                // PdfReader reader = new PdfReader(path + fileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
